import { Http } from '@/@nasty/Axios';
import { util } from '@/@nasty/Util';
import Upload from '@/@nasty/components/Upload';
import { PlusOutlined } from '@ant-design/icons';
import { ActionType, ModalForm, ProFormInstance, ProFormItem, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { FormattedMessage, useIntl, useRequest } from '@umijs/max';
import { Button, message } from 'antd';
import { FC, cloneElement, useCallback, useRef, useState } from 'react';


async function SaveUser(options: any) {
    return new Promise<any>((resolve, reject) => {
        Http.post("/System/User/SaveUser", { ...options })
            .then((data) => {
                if (data.IsSuccess !== true)
                    reject(data.Message);
                else
                    resolve(data);
            }).catch((e) => {
                reject(e)
            })
    });
}


export const UpdatePasswordModel: FC<any> = (props) => {
    const { reload } = props;
    const { values, trigger } = props;
    const formRef = useRef<ProFormInstance>();
    const [messageApi, contextHolder] = message.useMessage();

    /**
     * @en-US International configuration
     * @zh-CN 国际化配置
     * */
    const intl = useIntl();
    const { run, loading } = useRequest(SaveUser, {
        manual: true,
        onSuccess: () => {
            messageApi.success('提交成功');
            reload?.();
        },
        onError: (e: any) => {
            messageApi.error(e);
        },
    });


    const load = () => {
        Http.post("/System/User/GetCurrentUserInfo", {}).then((res) => {
            if (res.IsSuccess) {
                let data = res.Data;
                data.Password = null;
                formRef.current?.setFieldsValue(data);
            } else {
                message.error("用户数据加载失败");
            }

        }).catch((error) => {
            message.error(error);
        })
    }


    return (
        <>
            {contextHolder}
            <ModalForm
                title={"修改密码"}
                formRef={formRef}
                trigger={trigger}
                width="552px"
                modalProps={{ okButtonProps: { loading } }}
                onFinish={async (value) => {
                    await run({ Id: value.Id, Password: value.Password });
                    return true;
                }}
                onOpenChange={(v) => {
                    if (v) load();
                }}


            >
                <ProFormText hidden={true} name="Id" />

                <ProFormText label="用户账号" name="Account" width="xl" disabled={true} />

                <ProFormText label="用户名称" name="Name" width="xl" disabled={true} />

                <ProFormText label="用户密码" name="Password" width="xl" placeholder={"保持密码为空将不更改密码"} />
            </ModalForm>
        </>
    );
};
